package org.csu.onlineedubackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.onlineedubackend.entity.CourseBookmark;
import org.csu.onlineedubackend.mapper.CourseBookmarkMapper;
import org.csu.onlineedubackend.service.CourseBookmarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;

@Service
@Slf4j
public class CourseBookmarkServiceImpl implements CourseBookmarkService {

    @Autowired
    private CourseBookmarkMapper courseBookmarkMapper;

    @Override
    public boolean addBookmark(Integer studentId, Integer courseId) {
        log.info("添加收藏 - 学生ID: {}, 课程ID: {}", studentId, courseId);
        
        // 检查是否已收藏
        if (isBookmarked(studentId, courseId)) {
            log.info("已经收藏过该课程 - 学生ID: {}, 课程ID: {}", studentId, courseId);
            return true; // 已经收藏过了
        }
        
        try {
            // 创建新的收藏记录
            CourseBookmark bookmark = new CourseBookmark();
            bookmark.setStudentId(studentId);
            bookmark.setCourseId(courseId);
            bookmark.setCreatedAt(LocalDateTime.now());
            
            // 插入数据库
            int result = courseBookmarkMapper.insert(bookmark);
            log.info("收藏记录插入结果 - 学生ID: {}, 课程ID: {}, 结果: {}", studentId, courseId, result > 0 ? "成功" : "失败");
            return result > 0;
        } catch (Exception e) {
            log.error("添加收藏异常 - 学生ID: {}, 课程ID: {}, 异常: {}", studentId, courseId, e.getMessage(), e);
            return false;
        }
    }

    @Override
    public boolean removeBookmark(Integer studentId, Integer courseId) {
        log.info("删除收藏 - 学生ID: {}, 课程ID: {}", studentId, courseId);
        
        try {
            // 构建查询条件
            QueryWrapper<CourseBookmark> wrapper = new QueryWrapper<>();
            wrapper.eq("student_id", studentId).eq("course_id", courseId);
            
            // 删除记录
            int result = courseBookmarkMapper.delete(wrapper);
            log.info("删除收藏结果 - 学生ID: {}, 课程ID: {}, 结果: {}", studentId, courseId, result > 0 ? "成功" : "失败");
            return result > 0;
        } catch (Exception e) {
            log.error("删除收藏异常 - 学生ID: {}, 课程ID: {}, 异常: {}", studentId, courseId, e.getMessage(), e);
            return false;
        }
    }

    @Override
    public boolean isBookmarked(Integer studentId, Integer courseId) {
        log.info("检查是否已收藏 - 学生ID: {}, 课程ID: {}", studentId, courseId);
        
        try {
            // 构建查询条件
            QueryWrapper<CourseBookmark> wrapper = new QueryWrapper<>();
            wrapper.eq("student_id", studentId).eq("course_id", courseId);
            
            // 查询记录数
            Long count = courseBookmarkMapper.selectCount(wrapper);
            log.info("收藏查询结果 - 学生ID: {}, 课程ID: {}, 是否已收藏: {}", studentId, courseId, count > 0);
            return count > 0;
        } catch (Exception e) {
            log.error("查询收藏状态异常 - 学生ID: {}, 课程ID: {}, 异常: {}", studentId, courseId, e.getMessage(), e);
            return false;
        }
    }
}